Component({
  /**
   * 组件的属性列表
   */
  properties: {
    active: 'home',
    endTime: { //结束时间
      type: Number,
      value: 1675003542
    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    flags: false,
    listData: null,
    timeIntervalSingle: null,
    txtTime: {
      hou:"00",
      min:"00",
      sec:"00",
    },
    iszhibo: false
  },
  observers: {
    "active": function (val) {
      if (val == 'service' || val == 'home' || val == 'person' || val == 'plan') {
        const that = this;
        const api = require("../../api/api");
        api.video.getliveUrl({
          get_one: 1,
          success(resV) {
            let videoPath = resV.data;
            if (videoPath.code == 0) {
              // videoPath.data = {
              //   "aa":1
              // }
              if (Object.keys(videoPath.data).length == 0) {
                that.setData({
                  listData: videoPath.data,
                  flags: false
                });
              } else {
                that.properties.endTime = videoPath.data.start_time;
                let timeIntervalSingle = setInterval(() => {
                  that.countdown()
                }, 1000)
                that.setData({
                  listData: videoPath.data,
                  flags: true,
                  timeIntervalSingle
                });
              }
            }
          }
        })
      }
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    timeFormat(param) {
      return param < 10 ? '0' + param : param;
    },
    //倒计时
    countdown: function () {
      let that = this;
      let time = 0;
      let obj = {};
      let endTime = that.properties.endTime * 1000; //new Date(that.properties.endTime.replace(/-/g, "/")).getTime();//结束时间时间戳
      let currentTime = new Date().getTime(); //当前时间时间戳
      time = (endTime - currentTime) / 1000;
      // 如果直播未开始
      if (time > 0) {
        let hou = parseInt(time / (60 * 60));
        let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
        let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
        obj = {
          hou: that.timeFormat(hou),
          min: that.timeFormat(min),
          sec: that.timeFormat(sec)
        }
      } else { //直播已开始
        obj = {
          hou: "00",
          min: "00",
          sec: "00"
        }
        this.setData({
          iszhibo: true,
        })
        clearTimeout(that.data.timeIntervalSingle); //清除定时器
      }
      this.setData({
        txtTime: obj,
      })
    },
    clickbtn() {
      //  let room =  wx.getStorageSync('home');
      let roomId = [this.data.listData.roomid];
      console.log(roomId);
      let customParams = encodeURIComponent(JSON.stringify({
        path: 'pages/index/index',
        pid: 1
      }));
      wx.navigateTo({
        url: `plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${roomId}&custom_params=${customParams}`
      })
    },
  },
  lifetimes: {
    attached() {
      const that = this;
      const api = require("../../api/api");
      api.video.getliveUrl({
        get_one: 1,
        success(resV) {
          let videoPath = resV.data;
          if (videoPath.code == 0) {
            // videoPath.data = {
            //   "aa":1
            // }
            if (Object.keys(videoPath.data).length == 0) {
              that.setData({
                listData: videoPath.data,
                flags: false
              });
            } else {
              that.properties.endTime = videoPath.data.start_time;
              let timeIntervalSingle = setInterval(() => {
                that.countdown()
              }, 1000)
              that.setData({
                listData: videoPath.data,
                flags: true,
                timeIntervalSingle
              });
            }
          }
        }
      })
    }
  }
})